﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace LWrap.Index
{
    public class IndexWriter:IDisposable
    {
        private string _indexPath;
        private bool _isCreateNew;
        Lucene.Net.Store.Directory _directory;
        Lucene.Net.Analysis.Analyzer _analyzer;
        Lucene.Net.Index.IndexWriter _indexWriter;

        public string IndexFilePath
        {
            get { return _indexPath; }
            set { _indexPath = value; }
        }

        public bool IsCreateNewIndex
        {
            get { return _isCreateNew; }
            set { _isCreateNew = value; }
        }
        public IndexWriter() { }
        public IndexWriter(string indexPath, bool isCreateNew)
        {
            _indexPath = indexPath;
            _isCreateNew = isCreateNew;
            _directory = Lucene.Net.Store.FSDirectory.GetDirectory(_indexPath, _isCreateNew);
            _analyzer = new Lucene.Net.Analysis.Standard.StandardAnalyzer();

            //create the index writer with the directory and analyzer defined.
            _indexWriter = new Lucene.Net.Index.IndexWriter(_directory, _analyzer,/*true to create a new index*/ _isCreateNew);

        }
        internal void Execute(Lucene.Net.Documents.Document document)
        {
            _indexWriter.AddDocument(document);
        }


        public void Dispose()
        {
            _analyzer.Close();
            _indexWriter.Close();
            _directory.Close();
            
        }
    }
}
